package org.jeecg.modules.demo.sy.controller;

import org.jeecg.modules.demo.sy.entity.*;
import org.jeecg.modules.demo.sy.service.DeviceService;
import org.jeecg.modules.demo.sy.utils.JsonBack;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;


/**
 * @Description: TODO
 * @author: scott
 * @date: 2022年03月04日 10:20
 */
@RestController
@RequestMapping("/device")
public class DeviceController {

    @Autowired
    DeviceService deviceService;

    /*查询所有的设备的信息*/
    @GetMapping("/findAllDevices")
    public JsonBack findAllDevices()
    {
        List<Device> deviceList = deviceService.findAllDevices();
        if(deviceList != null)
        {
            return new JsonBack(200,"查询成功",deviceList);
        }else{
            return new JsonBack(404,"未查询到数据");
        }
    }

    /*查询一共有多少类型的设备以及该设备的数量*/
    @GetMapping("/findDevicesStatusAndAmount")
    public JsonBack findDevicesStatusAndAmount()
    {
        List<Mode1> mode1List = deviceService.findDevicesStatusAndAmount();
        if(mode1List != null)
        {
            return new JsonBack(200,"查询成功",mode1List);
        }else{
            return new JsonBack(404,"未查询到数据");
        }
    }

    /*查询一共有多少类型的设备以及该设备的数量*/
    @GetMapping("/findDevicesTypeAndAmount")
    public JsonBack findDevicesTypeAndAmount()
    {
        List<Mode1> mode1List = deviceService.findDevicesTypeAndAmount();
        if(mode1List != null)
        {
            return new JsonBack(200,"查询成功",mode1List);
        }else{
            return new JsonBack(404,"未查询到数据");
        }
    }
    /*查询一共有多少个设备*/
    @GetMapping("/findDevicesAmount")
    public JsonBack findDevicesAmount()
    {
        Integer DevicesAmount = deviceService.findDevicesAmount();
        if(DevicesAmount != null)
        {
            return new JsonBack(200,"查询成功",DevicesAmount);
        }else{
            return new JsonBack(404,"未查询到数据");
        }
    }
    /*查询一共有多少个在线的设备*/
    @GetMapping("/findOnlineDevicesAmount")
    public JsonBack findOnlineDevicesAmount()
    {
        Integer OnlineDevicesAmount = deviceService.findDevicesAmountByStatusId(1);
        if(OnlineDevicesAmount != null)
        {
            return new JsonBack(200,"查询成功",OnlineDevicesAmount);
        }else{
            return new JsonBack(404,"未查询到数据");
        }
    }
    /*查询一共有多少个离线的设备*/
    @GetMapping("/findOffLineDevicesAmount")
    public JsonBack findOffLineDevicesAmount()
    {
        Integer OffLineDevicesAmount = deviceService.findDevicesAmountByStatusId(2);
        if(OffLineDevicesAmount != null)
        {
            return new JsonBack(200,"查询成功",OffLineDevicesAmount);
        }else{
            return new JsonBack(404,"未查询到数据");
        }
    }
    /*查询一共有多少个故障的设备*/
    @GetMapping("/findFaultDevicesAmount")
    public JsonBack findFaultDevicesAmount()
    {
        Integer FaultDevicesAmount = deviceService.findDevicesAmountByStatusId(3);
        if(FaultDevicesAmount != null)
        {
            return new JsonBack(200,"查询成功",FaultDevicesAmount);
        }else{
            return new JsonBack(404,"未查询到数据");
        }
    }

    /*修改设备的状态--无返回值*/
    @GetMapping("/updateDevicesStatus")
    public void updateDevicesStatus(int deviceId,int nowDeviceStatusId,int nowFaultTypeId)
    {
        deviceService.updateDevicesStatus(deviceId,nowDeviceStatusId,nowFaultTypeId);
    }

    /*查询某地区的设备总数以及故障设备的总数*/
    @GetMapping("/findAreaDevicesAmountAndFaultDeviceAmount")
    public JsonBack findAreaDevicesAmountAndFaultDeviceAmount()
    {
        List<Mode2> mode2List = deviceService.findAreaDevicesAmountAndFaultDeviceAmount();
        List<Mode3> mode3List = new ArrayList<>();
        for(int i=0;i<mode2List.size();i++){
            //获取第i个元素
            Mode2 mode2 = mode2List.get(i);
            String areaName = mode2.getAreaName();
            Double faultValue = mode2.getFaultValue();
            Double totalValue = mode2.getTotalValue();
            Double proportion = faultValue/totalValue;

            //根据区域名获取区域id
            int deviceAreaId = deviceService.findAreaIdByName(areaName);

            Mode3 mode3 = new Mode3();
            mode3.setDeviceAreaId(deviceAreaId);
            mode3.setAreaName(areaName);
            mode3.setTotalLength(1);
            mode3.setProportion(proportion);
            mode3List.add(mode3);
            Date insertTime = new Date();
            deviceService.insertDeviceAreaLog(areaName,faultValue,totalValue,proportion,insertTime);

        }

        if(mode3List != null)
        {
            return new JsonBack(200,"查询成功",mode3List);
        }else{
            return new JsonBack(404,"未查询到数据");
        }
    }

    /*查询最近的所有地区的故障比例（由低到高）*/
    @GetMapping("/findExcellentAreaRank")
    public JsonBack findExcellentAreaRank()
    {
        Integer areaAmount = deviceService.findAreaAmount();
        List<Mode4> mode4List = deviceService.findExcellentAreaRank(areaAmount);
        List<Mode3> mode3List = new ArrayList<>();
        for(int i=0;i<mode4List.size();i++){
            //获取第i个元素
            Mode4 mode4 = mode4List.get(i);
            String areaName = mode4.getAreaName();
            Double proportion = mode4.getProportion();


            Mode3 mode3 = new Mode3();
            mode3.setDeviceAreaId(i+1);  //排序id
            mode3.setAreaName(areaName);
            mode3.setTotalLength(1);
            mode3.setProportion(proportion);
            mode3List.add(mode3);
        }
        if(mode3List != null)
        {
            return new JsonBack(200,"查询成功",mode3List);
        }else{
            return new JsonBack(404,"未查询到数据");
        }
    }







}
